import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.*;
import java.sql.*;

public class editAppointmentsFrame extends JDialog
{
 
  /* Frame */
  editAppointmentsFrame frame;
  Canvas canvas;
  JPanel panel;
  JLabel title;
  ArrayList <Integer> servicepicked = new ArrayList();
  ArrayList <Integer> patientpicked = new ArrayList();
  
  JComboBox service;
  JComboBox patient;
  JTextField date;
  JTextField startTime;
  JTextField endTime;
  JComboBox status;
  JTextArea progress;
  JScrollPane scrollPane;
  JTextArea comments;
  JScrollPane commentsPane;
  
  JButton btnSaveChanges;
  JButton btnCancel;
  
  JLabel backgroundImage;
  
  public editAppointmentsFrame(JFrame frame, String str){
            super(frame, str);
            addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent evt){
                    MainFrame.frame.setEnabled(true);
                }
            });
    }
    
  public void MainFrame(){
      
        /* Frame Options */
        frame = new editAppointmentsFrame(new JFrame(),"Edit Appointment Details");
        frame.setSize(370,540);

        /* Panel Options*/
        panel = new JPanel();
        panel.setPreferredSize(new Dimension(370, 540));
        panel.setLayout(null);
        
            title = new JLabel("Edit Appointment Details ");
            title.setFont(new Font("Microsoft Sans Serif", Font.PLAIN, 16));
            title.setForeground(Color.white);
            title.setBounds(20,0,270,30);
            panel.add(title);
        
            title = new JLabel("Service Type: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(20,20,170,50);
            panel.add(title);
       
            title = new JLabel("Patient: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(20,55,170,50);
            panel.add(title);
       
            title = new JLabel("Date: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(20,90,250,50);
            panel.add(title);
            
            title = new JLabel("Start Time: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(20,125,250,50);
            panel.add(title);
            
            title = new JLabel("End Time: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(20,160,250,50);
            panel.add(title);
            
            title = new JLabel("Status: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(20,195,250,50);
            panel.add(title);
            
            title = new JLabel("Progress: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(20,230,250,50);
            panel.add(title);
            
            title = new JLabel("Comment/s: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(20,340,250,50);
            panel.add(title);
            

            
            try{
                        /* Service Combobox */
                        MainFrame.database.stmt = MainFrame.database.conn.createStatement();
                        MainFrame.database.rs = MainFrame.database.stmt.executeQuery("SELECT * FROM SERVICE ORDER BY SERVICENAME");
                               
                        servicepicked.clear();
                        service = new JComboBox();
                        while(MainFrame.database.rs.next() ){
                           servicepicked.add(MainFrame.database.rs.getInt("serviceId"));
                           service.addItem(MainFrame.database.rs.getString("serviceName"));
                        }
                        
                        
                        /* Patient Combobox */
                        MainFrame.database.stmt = MainFrame.database.conn.createStatement();
                        MainFrame.database.rs = MainFrame.database.stmt.executeQuery("SELECT * FROM PATIENT ORDER BY LASTNAME");
                               
                        patientpicked.clear();
                        patient = new JComboBox();
                        while(MainFrame.database.rs.next() ){
                           patientpicked.add(MainFrame.database.rs.getInt("patientId"));
                           patient.addItem(MainFrame.database.rs.getString("lastName")+", "+MainFrame.database.rs.getString("firstName"));
                        }
                        
                        
                        MainFrame.database.stmt = MainFrame.database.conn.createStatement();
                        MainFrame.database.rs = MainFrame.database.stmt.executeQuery( 
                                "SELECT * FROM APPOINTMENT p,PATIENT q, SERVICE r WHERE p.SERVICEID = r.SERVICEID" 
                                + " AND p.PATIENTID = q.PATIENTID AND p.APPOINTMENTID =" + appointmentsPage.picked);
                               
                        while(MainFrame.database.rs.next() ){
                             /* Date input */
                           date = new JTextField(MainFrame.database.rs.getString("appointmentDate"));
                           date.setBounds(150,105,190,25);
                           date.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
                           panel.add(date);
            
                             /* Start Time input */
                           startTime = new JTextField(MainFrame.database.rs.getString("startTime"));
                           startTime.setBounds(150,140,190,25);
                           startTime.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
                           panel.add(startTime);
            
                             /* End Time input */
                           endTime = new JTextField(MainFrame.database.rs.getString("endTime"));
                           endTime.setBounds(150,175,190,25);
                           endTime.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
                           panel.add(endTime);
                           
                           /*Status Combobox */
                           status = new JComboBox();
                           status.addItem("Pending");
                           status.addItem("Late");
                           status.addItem("Completed");
                           if(MainFrame.database.rs.getString("status").equals("Pending"))
                                status.setSelectedItem("Pending");
                           else if(MainFrame.database.rs.getString("status").equals("Late"))
                                status.setSelectedItem("Late");
                           else if(MainFrame.database.rs.getString("status").equals("Completed"))
                                status.setSelectedItem("Completed");
                           status.setBackground(Color.WHITE);
                           status.setBounds(150,210,190,25);
                           status.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
                           panel.add(status);
                           
                           /* Progress Input */
                           progress = new JTextArea(MainFrame.database.rs.getString("progress"));
                           progress.setEditable(true);
                           progress.setLineWrap(true);
                           progress.setWrapStyleWord(true);
                           progress.setFont(new Font("Cambria", Font.PLAIN, 14));
                           scrollPane = new JScrollPane(progress);
                           scrollPane.setBounds(150,245,190,100);
                           scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
                           panel.add(scrollPane);
       
                           /* Comments Input */
                           comments = new JTextArea(MainFrame.database.rs.getString("commentsAndSuggestions"));
                           comments.setEditable(true);
                           comments.setLineWrap(true);
                           comments.setWrapStyleWord(true);
                           comments.setFont(new Font("Cambria", Font.PLAIN, 14));
                           commentsPane = new JScrollPane(comments);
                           commentsPane.setBounds(150,355,190,100);
                           commentsPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
                           panel.add(commentsPane);
                             
                           patient.setSelectedIndex(patientpicked.indexOf(MainFrame.database.rs.getInt("patientId")));
                           patient.setBackground(Color.WHITE);
                           patient.setBounds(150,70,190,25);
                           patient.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
                           panel.add(patient);
                           
                           service.setSelectedItem(MainFrame.database.rs.getString("serviceName"));
                           service.setBackground(Color.WHITE);
                           service.setBounds(150,35,190,25);
                           service.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
                           panel.add(service);
                        }
                        
                } catch( Exception ex ){}
            
            /* Save Changes Button */
            btnSaveChanges = new JButton(new ImageIcon("saveChangesButton.jpg"));
            btnSaveChanges.setBounds(50,470,114,25);
            btnSaveChanges.addActionListener(new ButtonListener());
            panel.add(btnSaveChanges);
            
            /* Cancel Button */
            btnCancel = new JButton(new ImageIcon("cancelButton.jpg"));
            btnCancel.setBounds(174,470,114,25);
            btnCancel.addActionListener(new ButtonListener());
            panel.add(btnCancel);

            /* Background Image */
            backgroundImage = new JLabel(new ImageIcon("bgedit2.jpg"));  
            backgroundImage.setBounds(0,0,370,540);
            panel.add(backgroundImage);
        
            /* Frame Operations */
            frame.getContentPane().add(panel);
            frame.setLocationRelativeTo(null);
            frame.setResizable(false);
            frame.setVisible(true);
  }
  
    public class ButtonListener implements ActionListener{

     public void actionPerformed(ActionEvent e){
       if(e.getSource() == btnSaveChanges){
           // Save changes made.
            try{
                        MainFrame.database.stmt = MainFrame.database.conn.createStatement();
                        String avail;
                        
                                 int numUpdated = 
                                 MainFrame.database.stmt.executeUpdate( "UPDATE APPOINTMENT SET appointmentDate = '" + date.getText() + "', startTime = '" 
                                 + startTime.getText() + "', endTime = '" + endTime.getText() + "', progress = '" + progress.getText() + "', commentsAndSuggestions = '" 
                                 + comments.getText() + "', status = '" + status.getSelectedItem() + "', patientId = " + patientpicked.get(patient.getSelectedIndex()) + ", serviceId = " 
                                 + servicepicked.get(service.getSelectedIndex()) + " WHERE appointmentId = " + appointmentsPage.picked);
                        
                                 JOptionPane.showMessageDialog(null,"All changes made are successfully saved.");
           
                                 frame.dispose();
           
                                 MainFrame.panel.removeAll();
                                 MainFrame.appointmentsPage.Draw();
                                 MainFrame.header.Draw();
                                 MainFrame.panel.add(MainFrame.backgroundImage);
                                 MainFrame.panel.repaint();
                                 
                                 MainFrame.frame.setEnabled(true);
                                 MainFrame.frame.toFront();
                                 MainFrame.frame.setState(Frame.NORMAL);
            } catch( SQLException se ){}
           
        }
       else if(e.getSource() == btnCancel){
           frame.dispose();
           MainFrame.frame.setEnabled(true);
           MainFrame.frame.toFront();
           MainFrame.frame.setState(Frame.NORMAL);
        }
    }
   }
  }  

